package com.jacobson.huobi.components.header

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.jacobson.huobi.R
import kotlinx.android.synthetic.main.view_preference_header.view.*

/**
 * ================================================
 *
 *  @author Wucz on 2018-08-31 16:05
 * ================================================
 */

class PreferenceHeader @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var confirmListener: (() -> Unit)? = null
    private var title: String = ""

    override fun onFinishInflate() {
        super.onFinishInflate()
        LayoutInflater.from(context).inflate(R.layout.view_preference_header, this, true)
        preferenceTvCancel.setOnClickListener {
            (context as? Activity)?.finish()
        }

        preferenceTvConfirm.setOnClickListener {
            confirmListener?.invoke()
        }

        setTitle(title)
    }

    fun setTitle(title: String) {
        this.title = title
        preferenceTvTitle?.text = title
    }

    fun setConfirmListener(listener: (() -> Unit)? = null) {
        this.confirmListener = listener
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setPadding(paddingLeft, paddingTop, paddingRight, 0)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    fun setConfirmEnable(enable: Boolean) {
        preferenceTvConfirm.isEnabled = enable
        if (enable) {
            preferenceTvConfirm.setTextColor(Color.WHITE)
        } else {
            preferenceTvConfirm.setTextColor(Color.parseColor("#f65555"))
        }
    }


}